/**
 * 业务指令类
 */

import { log } from '../aop/log';
import { FileOptions, TTSTalk } from '../interface';

interface IBusiness {
	id: string;
	name: IBusinessName;
	status: IBusinessStatus;
	clientId: string;
	username: string;
	userRole: string;
	cmdId: number | null;
}

// OCR 结果数据结果
interface OCRResult {
	id: string;
	status: string;
	data: any;
	image: string;
}

/** 指令枚举 */
enum IBusinessName {
	SCREEN_SHOT = 'screenshot',
	SCREEN_SHARE = 'screenShare',
	SEND_LINK = 'sendLink',
	SEND_VIDEO_LINK = 'sendVideoLink',
	SEND_SCANNER_CODE = 'sendScannerCode',
	CANCEL_SCANNER_CODE = 'cancelScannerCode',
	REQUEST_GEO_LOCATION = 'requestGeoLocation',
	FACE_COMPARE = 'faceCompare',
	SNAP_SHOT = 'snapShot',
	RECORD_SCREEN = 'recordScreen',
	CARD_COMPARE = 'cardCompare',
	SCREEN_MARK = 'screenMark',
	GET_GEO_LOCATION = 'getGeoLocaltion',
	SEND_SIGN = 'sendSign',
	FILL_FORM = 'fillForm',
	SEND_PDF = 'sendPDF',
	SEND_CARD_COLLECTION = 'sendCardCollection',
	SEND_IDCARD_COLLECTION = 'sendIdCardCollection',
	SEND_BANKCARD_COLLECTION = 'sendBankCardCollection',
	SHARED_WHITEDOARD = 'sharedWhiteboard',
	SHARED_PDF = 'sharedPDF',
	SEND_VARIABLE_COLLECTION = 'sendVariableCollection',
	TRANSCRIPT_OF_TESTIMONY = 'transcriptOfTestimony',
	SEND_VIDEO_COLLECTION = 'sendVideoCollection',
	TTS_BROADCAST = 'ttsBroadcast',
	REGIONAL_SCREEN_RECORDING = 'regionalScreenRecording',
	SEND_KNOWLEDGE_BASE = 'sendKnowledgeBase',
	CUSTOMER_VIEW_MARK = 'customerViewMark'
}

enum IBusinessStatus {
	IDLE = 'idle',
	START = 'start',
	IN_PROGRESS = 'in-progress',
	REJECTED = 'rejected',
	CANCEL = 'cancel',
	FAILED = 'failed',
	DONE = 'done'
}

interface ICompletedProps {
	task: IBusinessName;
	data: IBusiness;
	response: any;
	isRead: boolean;
}

export interface IFormBusiness {
	clientId: string;
	status: IBusinessStatus;
	forms: IForm[];
}

export interface IForm {
	followId: string;
	formId: string;
	formName: string;
	formTemplate: string;
	title: string;
	detailId: number;
	createdAt: string;
	disabled?: boolean;
}

class BusinessDao {
	private static completedTask: ICompletedProps[] = [];
	private static BusinessQuene: Map<string, IBusiness> = new Map();
	private static BusinessCompletedQuene: Map<string, ICompletedProps> = new Map();
	private static BusinessFormQuene: ICompletedProps[] = [];
	private static OcrResultList: OCRResult[] = []; // OCR结果数据列表
	private static historyBusinessQuene: Map<string, IBusiness> = new Map();
	private static pdfFiles: FileOptions[] = [];
	private static videoFiles: FileOptions[] = [];
	private static pushFormBusiness: Map<string, IFormBusiness[]> = new Map();

	private static TTSTalks: TTSTalk[] = [];

	@log('日志消息')
	public static addBusiness(business: IBusiness) {
		if (this.BusinessQuene.has(business.name)) return;
		this.BusinessQuene.set(business.name, business);
		return this.BusinessQuene;
	}

	@log('日志消息')
	public static removeBusiness(businessName: string) {
		if (this.BusinessQuene.has(businessName)) {
			this.BusinessQuene.delete(businessName);
		}
	}

	@log('日志消息')
	public static findBusiness(businessName: string) {
		if (this.BusinessQuene.has(businessName)) {
			return this.BusinessQuene.get(businessName);
		}
	}

	@log('日志消息')
	public static updateBusiness(business: IBusiness) {
		this.BusinessQuene.set(business.name, business);
		return this.BusinessQuene;
	}

	@log('日志消息')
	public static findBusinessWithStatus(status: IBusinessStatus) {
		const business = [...this.BusinessQuene.values()].filter(item => item.status === status);
		return business;
	}

	@log('日志消息')
	public static clearBusiness() {
		this.BusinessQuene.clear();
	}

	@log('日志消息')
	public static getAllBusiness() {
		return this.BusinessQuene.values();
	}

	public static addCompletedBusiness(business: IBusiness) {
		// this.BusinessCompletedQuene.set(business.name, business);
	}

	public static setPdfFiles(files: FileOptions[]) {
		this.pdfFiles = files;
	}

	public static getPdfFiles() {
		return this.pdfFiles;
	}

	public static setVideoFiles(files: FileOptions[]) {
		this.videoFiles = files;
	}
	public static getVideoFiles() {
		return this.videoFiles;
	}

	public static setTTSTalks(talks: TTSTalk[]) {
		this.TTSTalks = talks;
	}

	public static getTTSTalks() {
		return this.TTSTalks;
	}

	public static getFormDetail(followId: string) {
		return this.pushFormBusiness.get(followId) || [];
	}

	public static insertFormDetail(clientId: string, form: IFormBusiness) {
		if (this.pushFormBusiness.has(clientId)) {
			let businessforms = this.pushFormBusiness.get(clientId)!;
			this.pushFormBusiness.set(clientId, [form, ...businessforms]);
		} else {
			this.pushFormBusiness.set(clientId, [form]);
		}

		console.log('insertFormDetail:', this.pushFormBusiness);
	}

	public static clearFormDetail() {
		this.pushFormBusiness.clear();
	}

	public static getClientForms(clientId: string) {
		return this.pushFormBusiness.get(clientId) || [];
	}
}

export default BusinessDao;
